//
//  GZResetPasswordOneViewController.swift
//  GZMarathon
//
//  Created by apple on 2021/11/11.
//

import UIKit
import SVProgressHUD
class GZResetPasswordOneViewController: EBBaseViewController {

    @IBOutlet weak var nextBt: UIButton!
    @IBOutlet weak var phoneTF: UITextField!
    
    
    @IBOutlet weak var vericationView: UIView!
    @IBOutlet weak var vericationTF: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        phoneTF.delegate = self
        vericationTF.delegate = self

        self.title = "重置密码"
        
        let btn1 = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
        btn1.setImage(UIImage(named: "back"), for: .normal)
        btn1.addTarget(self, action: #selector(backClick), for: .touchUpInside)
        let item1 = UIBarButtonItem(customView: btn1)
        self.navigationItem.leftBarButtonItem = item1

        setUI()
       
    }
    
    @objc func backClick(){
        self.navigationController?.popViewController(animated: true)
    }
    
    
    func setUI(){
        
        
        let leftview = UIImageView(image: UIImage(named: "icon_denglu_mima"))
        leftview.frame = CGRect(x: 0, y: 0, width: 18, height: 18)
        phoneTF.leftViewMode = UITextField.ViewMode.always
        phoneTF.leftView = leftview
        
        let leftview1 = UIImageView(image: UIImage(named: "icon_denglu_yanzhengma"))
        leftview1.frame = CGRect(x: 0, y: 0, width: 18, height: 18)
        vericationTF.leftViewMode = UITextField.ViewMode.always
        vericationTF.leftView = leftview1

        
        let btn = TimerButton()
        btn.backgroundColor = UIColor(red: 46.0/255.0, green: 75.0/255.0, blue: 244.0/255.0, alpha: 0.1)
        
        vericationView.addSubview(btn)
        
        btn.setup("获取验证码", timeTitlePrefix: "")
        
        btn.snp.makeConstraints { make in
            make.top.bottom.left.right.equalToSuperview()
        }
        btn.clickBtnEvent = { [self]
            () -> Void in
                if !btn.isWorking{

                    ApiProvider.request(EBAPI.smscaptcha(phone_num: phoneTF.text ?? ""), model: BaseModel.self) { res, isSuccess in
                        if(isSuccess == true){
                            
                            SVProgressHUD.showSuccess(withStatus: res?.message)
                            btn.isWorking = true
                        }
                        
                    }
                }
        }
    }
    
    @IBAction func nextClick(_ sender: UIButton) {
        view.endEditing(true)
        
        if(vericationTF.text?.count == 0){
            SVProgressHUD.showError(withStatus: "验证码不能为空")
            return
        }
        if(phoneTF.text?.count == 0){
            SVProgressHUD.showError(withStatus: "手机号不能为空")
            return
        }
        
        let vc = UIStoryboard(name: "GM", bundle: nil).instantiateViewController(withIdentifier: "GZResetPasswordTwoViewController") as! GZResetPasswordTwoViewController
        vc.yzm = vericationTF.text ?? ""
        vc.phone_num = phoneTF.text ?? ""
        self.navigationController?.pushViewController(vc, animated: true)
    }


}
extension GZResetPasswordOneViewController:UITextFieldDelegate{
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if((vericationTF.text?.count != 0) && (phoneTF.text?.count != 0)){
            nextBt.backgroundColor = UIColor("#2E4BF4")
            nextBt.isUserInteractionEnabled = true
        }else{
            nextBt.isUserInteractionEnabled = false
            nextBt.backgroundColor = UIColor("#C1C9FC")
        }
    }
    
}
